import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/lib/theme-chalk/index.css'
import router from './router'
import store from './store'

createApp(App).use(store).use(router).use(ElementPlus).mount('#app')
// 钩子函数，访问路由前调用
router.beforeEach((to, from, next) => {
  // 路由需要认证
  if (to.meta.requireAuth) {
    // 判断store里是否有token
    if (store.state.userToken) {
      next()
    } else {
      next({
        path: 'login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})
